package Comp355Final.main;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * The GUI for text reader
 * @author chung
 *
 */
public class GUIReadText extends Activity{
	private static TextView txtview;
	private Config myconfig;
	private ScrollView scllview;
	private FileIO fileio;
	private String chapter_name;
	private String book_folder;
	private BookMark mybookmark;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	Bundle extras = getIntent().getExtras();
    	String dest = extras.getString("dest");
    	String charset = extras.getString("charset");
    	chapter_name = extras.getString("book_name");
    	book_folder = extras.getString("book_folder");

    	myconfig = new Config(this);

    	scllview = new ScrollView(this);
    	ScrollView.LayoutParams scllview_param = new ScrollView.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        scllview.setBackgroundColor(Color.WHITE);
        scllview.setBackgroundDrawable(myconfig.return_bg());
    	LinearLayout Llay = new LinearLayout(this);
        LinearLayout.LayoutParams Llay_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
    	Llay.setOrientation(LinearLayout.VERTICAL);
    	
    	fileio = new FileIO();

    	String hole_passage = fileio.textViewReadfileOP(dest, charset);
    	txtview = new TextView(this);
    	txtview.setText(hole_passage);
    	txtview.setTextColor(Color.BLACK);
    	txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float)17);
    	//txtview.bringPointIntoView(offset)
    	Llay.addView(txtview);
    	
    	mybookmark = new BookMark(this.fileio,this.scllview,chapter_name,book_folder);
    	HaveBookMark = mybookmark.GetTheBookMark();
    	if(HaveBookMark)
    	{
    		Utility.getAlertDialog("Last Bookmark found", "Do you want to load back the bookmark?", this, new DiologClickListener(this,mybookmark)).show();
    		//Utility.quick_dialog("Moving the scroll", this);
    		
    	}
    	//scllview.
        this.setTitle("Text reader - "+chapter_name);
        scllview.addView(Llay, Llay_param);
        this.setContentView(scllview, scllview_param);
    }
    private boolean HaveBookMark;
    private String[] All_color;
    private float[] All_txtsize;
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
	   menu.add(0, 0, 0, "Return to BookShelf").setIcon(android.R.drawable.ic_menu_revert);
	   SubMenu TxtSize =menu.addSubMenu(0, 1, 1, "Font Size").setIcon(android.R.drawable.ic_menu_zoom);
	   //ic_menu_zoom
	   //txtview.setTextColor(color);
	   SubMenu color = menu.addSubMenu(0, 2, 2, "Font Color").setIcon(android.R.drawable.ic_menu_edit);

	   menu.add(0, 3, 3, "BookMark").setIcon(android.R.drawable.ic_menu_save);
	   menu.add(0, 4, 4, "Play").setIcon(android.R.drawable.ic_menu_upload_you_tube);
	   All_color = Utility.returnAllColor();
	   
	   for(int i=0;i< All_color.length;i++)
	   {
		   color.add(0,5+i,0,All_color[i]);
	   }
	   All_txtsize = Utility.returnAllTxtSize();
	   
	   for(int i=0;i< All_txtsize.length;i++)
	   {
		   TxtSize.add(0,5+All_color.length+i,0,String.valueOf(All_txtsize[i]).substring(0, 2)+"px");
	   }
	   return super.onCreateOptionsMenu(menu); 
   }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	if(item.getItemId() >= 5 && item.getItemId()<(5+All_color.length))
    	{
    		txtview.setTextColor(Utility.return_actualColor(All_color[item.getItemId()-5]));
    	}
    	else if(item.getItemId()>=5+All_color.length)
    	{
    		Utility.quick_dialog("Text size changed to "+String.valueOf(All_txtsize[item.getItemId()-5-All_color.length]).substring(0,2)+"px", this);
    		txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP,All_txtsize[item.getItemId()-5-All_color.length]);
    	} 
    	//txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float)20);
    	switch(item.getItemId())
    	{
	    	case 0: 
				Intent int_ent = new Intent(this, GUIBookShelfBkSelect.class);
				this.startActivity(int_ent);
				this.finish();
	    		break;
	    	case 1:
	
	    		break;
	    	case 2:
	    		break;
	    	case 3:
	    		/*
	    		int scll_x = scllview.getScrollX();
	    		int scll_y = scllview.getScrollY();
	    		Utility.quick_dialog(" " +scll_x+" "+scll_y, this);
	    		scllview.scrollTo(scll_x, scll_y);
	    		*/
	    		
	    		mybookmark.SetTheBookMark();
	    		Utility.quick_dialog("Bookmark saved", this);
	    		break;
	    	case 4:
	    		
	    		break;
    	}
    	return super.onOptionsItemSelected(item);
    }
}
